package cn.ondraw.ragflow.controller;

import cn.ondraw.ragflow.pojo.request.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.common.core.domain.R;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.service.RagFlowKnowledgeBaseService;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/v1/kb")
public class RagFlowKnowledgeBaseController {

    private final RagFlowKnowledgeBaseService knowledgeBaseServiceRagFlow;

    /**
     * 创建知识库
     *
     * @param request 创建请求对象
     * @return 创建响应对象
     */
    @PostMapping("/create")
    public R<Object> create(@RequestBody KbCreateRequest request) {
        try {
            return R.ok(knowledgeBaseServiceRagFlow.create(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 更新知识库
     *
     * @param request 更新请求对象
     * @return 通用响应结果
     */
    @PostMapping("/update")
    public R<Object> update(@RequestBody KbUpdateRequest request) {
        try {
            return R.ok(knowledgeBaseServiceRagFlow.update(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取知识库详情
     *
     * @return 知识库详情
     */
    @GetMapping("/detail")
    public R<Object> detail(@RequestParam("kb_id") String kb_id) {
        try {
            return R.ok(knowledgeBaseServiceRagFlow.detail(kb_id));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 列出知识库
     *
     * @param request 列表请求对象
     * @return 知识库列表及总数
     */
    @PostMapping("/list")
    public R<Object> list(@RequestBody KbListRequest request) {
        try {
            return R.ok(knowledgeBaseServiceRagFlow.list(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除知识库
     *
     * @param request 删除请求对象
     * @return 通用响应结果
     */
    @PostMapping("/rm")
    public R<Object> rm(@RequestBody KbRmRequest request) {
        try {
            return R.ok(knowledgeBaseServiceRagFlow.rm(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 列出单个知识库的标签
     *
     * @param kbId 知识库 ID
     * @return 标签列表
     */
    @GetMapping("/{kbId}/tags")
    public R<Object> listTags(@PathVariable String kbId) {
        try {
            return R.ok(knowledgeBaseServiceRagFlow.listTags(kbId));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 列出多个知识库的标签
     *
     * @param request 标签请求对象
     * @return 标签列表
     */
    @GetMapping("/tags")
    public R<Object> listTagsFromKbs(@ModelAttribute KbTagsRequest request) {
        try {
            return R.ok(knowledgeBaseServiceRagFlow.listTagsFromKbs(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除知识库的标签
     *
     * @param request 删除标签请求对象
     * @return 通用响应结果
     */
    @PostMapping("/{kbId}/rm_tags")
    public R<Object> rmTags(@RequestBody KbRmTagsRequest request, @PathVariable String kbId) {
        try {
            request.setKbId(kbId);
            return R.ok(knowledgeBaseServiceRagFlow.rmTags(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 重命名知识库的标签
     *
     * @param request 重命名标签请求对象
     * @return 通用响应结果
     */
    @PostMapping("/{kbId}/rename_tag")
    public R<Object> renameTags(@RequestBody KbRenameTagRequest request, @PathVariable String kbId) {
        try {
            request.setKbId(kbId);
            return R.ok(knowledgeBaseServiceRagFlow.renameTags(request));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取知识库的知识图谱
     *
     * @param kbId 知识库 ID
     * @return 知识图谱数据
     */
    @GetMapping("/{kbId}/knowledge_graph")
    public R<Object> knowledgeGraph(@PathVariable String kbId) {
        try {
            return R.ok(knowledgeBaseServiceRagFlow.knowledgeGraph(kbId));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除知识库的知识图谱
     *
     * @param kbId 知识库 ID
     * @return 通用响应结果
     */
    @DeleteMapping("/{kbId}/knowledge_graph")
    public R<Object> deleteKnowledgeGraph(@PathVariable String kbId) {
        try {
            return R.ok(knowledgeBaseServiceRagFlow.deleteKnowledgeGraph(kbId));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}
